package com.ethan.cj.ssm.controller;

import com.ethan.cj.ssm.entity.Credit;
import com.ethan.cj.ssm.service.CreditGrantingService;
import com.ethan.cj.ssm.service.CreditService;
import com.ethan.cj.ssm.service.FirstTrialService;
import com.ethan.cj.ssm.service.ProductService;
import com.ethan.cj.ssm.util.DateUtil;
import com.ethan.cj.ssm.util.JsonUtil;
import lombok.extern.log4j.Log4j;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@Log4j
@RequestMapping("firsttrial")
public class FirstTrialController {
    /**
     * 全部贷款service层映射
     */
    @Autowired
    private CreditService cs;

    /**
     * 初审service层映射
     */
    @Autowired
    private FirstTrialService fts;

    /**
     * 授信机构service层映射
     */
    @Autowired
    private CreditGrantingService cgs;

    /**
     * 授信产品service层映射
     */
    @Autowired
    private ProductService ps;

    /**
     * 进入初审页面
     *
     * @return
     */
    @RequestMapping("/list")
    public String list() {
        return "lw/firsttrialhome";
    }

    /**
     * 进入初审审核
     *
     * @return
     */
    @RequestMapping("/trial")
    public String trial() {
        return "lw/updatefirsttrial";
    }

    /**
     * 全部贷款请求地址
     *
     * @return 路径
     */
    @RequestMapping("/firstTrialList")
    @ResponseBody
    public Map<Object, Object> firstTrialList(@RequestParam("page") int page, @RequestParam("limit") int limit, @RequestParam("name") String paramString) {
        Map map = JsonUtil.parseObject(paramString, Map.class);
        return cs.getCreditList(page, limit, map);
    }

    /**
     * 根据id 查询贷款初审信息
     *
     * @param cid
     * @param modelMap
     * @return
     */
    @RequestMapping("/selectCreditByCid")
    public String selectCreditByCid(int cid, ModelMap modelMap) {
        Credit c = fts.selectCreditByCid(cid);
        modelMap.addAttribute("c", c);
        return "/lw/updatefirsttrial";
    }

    /**
     * 修改初审状态
     *
     * @param cid    修改对象的ID
     * @param cstate 修改之后的状态
     * @return
     */
    @RequestMapping("/updateFirstTrial")
    @ResponseBody
    public Map<Object, Object> updateFirstTrial(@RequestParam("cid") int cid,
                                                @RequestParam("cgid") int cgid,
                                                @RequestParam("pid") int pid,
                                                @RequestParam("cstate") int cstate) {
        return fts.updateFirstTrial(cid, cgid, pid, cstate);
    }

    /**
     * 查询全部授信机构信息
     *
     * @return 授信机构的map集合
     */
    @RequestMapping("/selectCreditGranting")
    @ResponseBody
    public Map<Object, Object> selectCreditGranting() {
        return cgs.selectCreditGranting();
    }

    /**
     * 查询全部授信产品信息
     *
     * @return 授信产品的map集合
     */
    @RequestMapping("/selectProduct")
    @ResponseBody
    public Map<Object, Object> selectProduct(@RequestParam("cgid") int cgid) {
        return ps.selectProduct(cgid);
    }

    @RequestMapping("/isExport")
    public String isExport(@RequestParam("cids") String cids,
                           HttpServletResponse response) throws IOException {
        if (null != cids && !("".equals(cids))) {
            String cid[] = cids.split(",");
            List<Credit> list = new ArrayList<>();
            for (String id : cid) {
                Credit c = cs.selectCreditCid(Integer.valueOf(id), 1);
                list.add(c);
            }
            String title[] = {"初审ID", "申请时间", "客户姓名", "联系电话", "申请金额", "地址"};

            //创建Excel工作簿
            HSSFWorkbook workbook = new HSSFWorkbook();
            //创建一个工作表
            HSSFSheet sheet = workbook.createSheet();
            //创建第一行  sheet.createRow(0)表示第一行
            HSSFRow row = sheet.createRow(0);
            //列
            HSSFCell cell = null;
            for (int i = 0; i < title.length; i++) {
                //循环往第一行列中进行赋值
                cell = row.createCell(i);
                cell.setCellValue(title[i]);
            }
            //存储参数
            //循环创建其他行并对每行的列增加数据
            for (int i = 0; i < list.size(); i++) {
                HSSFRow nextrow = sheet.createRow(i + 1);
                HSSFCell dataCell = nextrow.createCell(0);
                dataCell.setCellValue(list.get(i).getCid());
                dataCell = nextrow.createCell(1);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                dataCell.setCellValue(sdf.format(list.get(i).getCtime()));
                dataCell = nextrow.createCell(2);
                dataCell.setCellValue(list.get(i).getCname());
                dataCell = nextrow.createCell(3);
                dataCell.setCellValue(list.get(i).getCphone());
                dataCell = nextrow.createCell(4);
                dataCell.setCellValue(list.get(i).getCmoney());
                dataCell = nextrow.createCell(5);
                dataCell.setCellValue(list.get(i).getCaddress());
            }
            OutputStream output = response.getOutputStream();
            response.reset();
            //导出路径,选择
            response.setHeader("Content-disposition", "attachment; filename=food.xls");
            response.setContentType("application/msexcel");
            workbook.write(output);
            output.close();
        }
        return "lw/firsttrialhome";
    }

}
